/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEngine;

namespace TFrameWork.Module.ActionEditor
{
    public class CustomOnTriggerTrack : CustomOnRecordTrack, IOnTriggerTrack
    {
        private Transform _playerRoot;
        private TriggerEvent _triggerEvent;

        public override void OnStart(TrackEventBase data)
        {
            base.OnStart(data);
            _triggerEvent = data as TriggerEvent;
#if UNITY_EDITOR
            //用于测试就使用编辑器加载
            _playerRoot = timeLineAction.GetComponent<Transform>();
#endif
            drawGizmos = true;
        }

        private bool drawGizmos = false;

        public override void OnUpdate(int currentFrame)
        {
            base.OnUpdate(currentFrame);
            if (_triggerEvent.recordEventDatas.TryGetValue(currentFrame, out var record))
            {
                
            }
        }

        public void OnDrawGizmos()
        {
#if UNITY_EDITOR
            if (!drawGizmos) return;
            if (_triggerEvent.recordEventDatas.TryGetValue(currentFrame, out var record))
            {
                if (eventData is BoxTriggerEvent box)
                {
                    var boxPos = box.position;
                    var boxRotation = Quaternion.Euler(box.rotate);
                    var boxScale = box.scale;
                    var boxLocalMat = Matrix4x4.TRS(boxPos, boxRotation, boxScale);
                    //使用录制的相对于角色根节点的变换矩阵，只需要知道根节点就能计算出当前对象的时间坐标，旋转和缩放。可以完全脱离显示层Transform的动画节点。
                    var mat = _playerRoot.transform.localToWorldMatrix * record.localToRelativeRootWrold * boxLocalMat;
                    Gizmos.matrix = mat;
                    var color = Gizmos.color;
                    Gizmos.color = new Color(0, 1, 0, 0.2f);
                    Gizmos.DrawCube(Vector3.zero, box.size);
                    Gizmos.color = new Color(1, 0, 0, 0.8f);
                    Gizmos.DrawWireCube(Vector3.zero, box.size);
                    Gizmos.color = color;

                }
                else if (eventData is SphereTriggerEvent sphere)
                {
                    var boxPos = sphere.position;
                    var boxRotation = Quaternion.Euler(sphere.rotate);
                    var boxScale = sphere.scale;
                    var boxLocalMat = Matrix4x4.TRS(boxPos, boxRotation, boxScale);
                    //使用录制的相对于角色根节点的变换矩阵，只需要知道根节点就能计算出当前对象的时间坐标，旋转和缩放。可以完全脱离显示层Transform的动画节点。
                    var mat = _playerRoot.transform.localToWorldMatrix * record.localToRelativeRootWrold * boxLocalMat;

                    var pos = mat.GetPosition();
                    var gColor2 = Gizmos.color;
                    Gizmos.color = new Color(0, 1, 0, 0.2f);
                    Gizmos.DrawSphere(pos, sphere.radius);
                    Gizmos.color = new Color(1, 0, 0, 0.8f);
                    Gizmos.DrawWireSphere(pos, sphere.radius);
                    Gizmos.color = gColor2;
                }

            }
#endif
        }

        public override void OnFinish()
        {
            drawGizmos = false;
        }
    }
}